package com.example.moban.application;

import android.app.Activity;
import android.content.Context;
import android.support.multidex.MultiDex;
import android.text.TextUtils;

//import com.baidu.mapapi.SDKInitializer;
//import com.hyphenate.chat.EMClient;
//import com.hyphenate.chat.EMOptions;
//import com.hyphenate.easeui.EaseUI;
import com.example.moban.base.PhoneTypeBean;
import com.example.moban.utils.MyUtils;
import com.liulishuo.filedownloader.FileDownloader;
import com.rzhd.coursepatriarch.common.base.BaseApplication;
import com.rzhd.coursepatriarch.common.utils.CommonUtil;
//import com.rzhd.coursepatriarch.umeng.UMUtils;
//import com.rzhd.coursepatriarch.utils.MyUtils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

//import cn.jpush.android.api.JPushInterface;

//import com.youzan.androidsdk.YouzanSDK;
//import com.youzan.androidsdkx5.YouZanSDKX5Adapter;
//import com.youzan.androidsdkx5.YouzanPreloader;

/**
 * @author yql
 * Time: 2018/6/6.
 */
public class MyApplication extends BaseApplication {
    private static MyApplication myApplication;
    private String[] bangScreenArr = {
            "Xiaomi_MI 8 Lite","Xiaomi_MI 9 SE","Xiaomi_MI 9","GALAXY_Note 9","HUAWEI_DUB-TL00","HUAWEI_COL-AL10"
    };
    private List<PhoneTypeBean.DataBean> phoneTypeList = new ArrayList<>();
    public  Activity splash;
    private List<Activity> activityList = new LinkedList<Activity>();

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install( this  );
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //shareSDK
        myApplication = this;

//        MobSDK.init(this);
        //腾讯bugLy崩溃日志（第三个参数为SDK调试模式开关）
//        CrashReport.initCrashReport(getApplicationContext(), "1641b8efcd", false);
//        //极光推送
//        JPushInterface.init(this);
////        设置调试模式
//        JPushInterface.setDebugMode(true);
////        友盟初始化
//        UMUtils.init(this);
//        //百度定位
//        SDKInitializer.initialize( this );
        //注意作者已经不建议使用init方法
        FileDownloader.setup(this);
        initHuanxinIM();
        initYouZan();
    }

    public static MyApplication getMyApplicationInstance() {
        return myApplication;
    }

    public void setPhoneTypeList(List<PhoneTypeBean.DataBean> phoneTypeList) {
        this.phoneTypeList = phoneTypeList;
    }

    /**
     * 初始化环信IM
     */
    public void initHuanxinIM(){
//        EMOptions options = new EMOptions();
//        // 默认添加好友时，是不需要验证的，改成需要验证
//        options.setAcceptInvitationAlways( true );
//        // 是否自动将消息附件上传到环信服务器，默认为True是使用环信服务器上传下载，如果设为 false，需要开发者自己处理附件消息的上传和下载
//        options.setAutoTransferMessageAttachments(true);
//        // 是否自动下载附件类消息的缩略图等，默认为 true 这里和上边这个参数相关联
//        options.setAutoDownloadThumbnail(true);
//        //是否自动登录
//        options.setAutoLogin( true );
//        //初始化
//        EMClient.getInstance().init( this, options);
////        EMClient.getInstance().callManager().getCallOptions().setIsSendPushIfOffline(true);
//        //初始化EaseUI---必须要
//        EaseUI.getInstance().init( this , options );
//        //在做打包混淆时，关闭debug模式，避免消耗不必要的资源
//        EMClient.getInstance().setDebugMode(true);
    }

    /**
     * 初始化有赞
     */
    public void initYouZan(){
        // 1.初始化SDK------------------------------
        // 腾讯X5版本使用
//         YouzanSDK.init(this, "392e85d10450f29b00", new YouZanSDKX5Adapter());

        // client_id即你在有赞申请的客户端标识（老用户用UA)

        // 2.(可选)预下载店铺主页------------------------------
//         YouzanPreloader.preloadHtml(this, HttpConstants.YOUZAN_SHOP_LINK );
        // 对html预加载时调用， 提升html打开速度，preload_html_url不能是重定向页面
    }

    /**
     * 判断是否是刘海屏
     * @return
     */
    public boolean isBangScreen(){

        if( bangScreenArr == null || bangScreenArr.length <= 0 ){
            return false;
        }
        String currentScreen = CommonUtil.getPhoneBrandAndModel();
        currentScreen = currentScreen.toLowerCase();
        if( TextUtils.isEmpty( currentScreen )){
            return false;
        }
        if( phoneTypeList == null || phoneTypeList.size() <= 0 ){
            for( int i = 0; i < bangScreenArr.length; i++ ){
                PhoneTypeBean.DataBean dataBean = new PhoneTypeBean.DataBean();
                dataBean.setId( i + 1 );
                dataBean.setName( bangScreenArr[ i ] );
                phoneTypeList.add( dataBean );
            }
        }
        for( int j = 0; j < phoneTypeList.size(); j++ ){
            PhoneTypeBean.DataBean dataBean = phoneTypeList.get(j);
            if( dataBean == null ){
                continue;
            }
            String str = dataBean.getName();
            if( TextUtils.isEmpty( str )){
                continue;
            }
            str = str.toLowerCase();
            if( str.equals( currentScreen )){
                return true;
            }
        }
        return false;
    }

    public int getPhoneType(){
//        return isBangScreen() ? 1 : 0;
        return hasNotchInScreen(splash) ? 1 : 0;
    }


    public  boolean hasNotchInScreen(Activity activity) {//是否是刘海屏
        return MyUtils.hasNotchInScreen(activity);
    }



    //添加Activity到容器中 
    public void addActivity(Activity activity) {
        activityList.add(activity);
    }
    //遍历所有Activity并finish 
    public void exit() {
        for(Activity activity:activityList) {
            activity.finish();
        }
        System.exit(0);
    }




}
